package com.isolate.run;

import com.zaxxer.nuprocess.NuProcess;
import com.zaxxer.nuprocess.NuProcessBuilder;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author gaozhongbao
 * @date 2020/08/06
 **/
@Slf4j
public class BaseProcessTest {
    ///usr/local/Cellar/python/3.7.4/bin/python3.7 /Users/koutadatomi/python/test.py
    public static void main(String[] args) throws Exception {
        NuProcessBuilder pb = new NuProcessBuilder(
                Arrays.asList("/usr/local/Cellar/python/3.7.4/bin/python3.7",
                        "/Users/koutadatomi/python/test.py"));

        TestHandler testHandler = new TestHandler();
        pb.setProcessListener(testHandler);
        NuProcess process = pb.start();


        ByteBuffer buffer = ByteBuffer.wrap("Hello,".getBytes());
        process.writeStdin(buffer);

        ByteBuffer buffer4 = ByteBuffer.wrap("World".getBytes());
        process.writeStdin(buffer4);

        ByteBuffer buffer1 = ByteBuffer.wrap("\r\n".getBytes());
        process.writeStdin(buffer1);

        ByteBuffer buffer2 = ByteBuffer.wrap("Hammy".getBytes());
        process.writeStdin(buffer2);

        ByteBuffer buffer3 = ByteBuffer.wrap("\r\n".getBytes());
        process.writeStdin(buffer3);

        process.waitFor(10, TimeUnit.SECONDS);


//        process.wantWrite();
//        Thread.sleep(10);
//        handler_new.setContent("\n");
//        process.wantWrite();
//        handler_new.setContent("凑合");
//        process.wantWrite();
//
//        handler_new.setContent("\n");
//        process.wantWrite();

//        process.waitFor(10, TimeUnit.SECONDS);


//        pb.setProcessListener(handler);
//        NuProcess process = pb.start();
//        process.wantWrite();
//
//        try {
//            process.waitFor(10, TimeUnit.SECONDS);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

}
